﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GMS.MainClasses;
using GMS.ServiceFunctions.Data;
using System.Data.SqlClient;
using GMS.MainForm;
using GMS.Enums;

namespace GMS.ViewUsers
{
    public partial class AddEditUser : ChildForms
    {
        DataGridView DGV;
        TextBox TB;
        public AddEditUser(EditMode FormMode)
        {
            InitializeComponent();
            this.FormMode = FormMode;
            StoredProcedures = new String[5];
            StoredProcedures[0] = "[SelectDatabaseUser]";
            StoredProcedures[1] = "[EditDatabaseUser]";
            StoredProcedures[2] = "[SelectServerUser]";
            StoredProcedures[3] = "[AddDatabaseUser]";
            StoredProcedures[4] = "[DeleteDatabaseUser]";
            if (FormMode == EditMode.Editing || FormMode == EditMode.Deleting)
            {
                PictureBox PB = new PictureBox();

                this.splitContainer1.Panel1.Controls.Add(PB);

                PB.Dock = System.Windows.Forms.DockStyle.Fill;
                PB.Image = global::GMS.ViewUsers.Properties.Resources.J0195384;
                PB.Location = new System.Drawing.Point(0, 0);
                PB.Name = "pictureBox1";
                PB.Size = new System.Drawing.Size(130, 240);
                PB.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
                PB.TabIndex = 10;
                PB.TabStop = false;

            }
            if (FormMode == EditMode.AddIn)
            {
                DGV = new DataGridView();
                TB = new TextBox();

                this.splitContainer1.Panel1.Controls.Add(DGV);
                this.splitContainer1.Panel1.Controls.Add(TB);

                DGV.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
            | System.Windows.Forms.AnchorStyles.Left)
            | System.Windows.Forms.AnchorStyles.Right)));
                DGV.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
                DGV.Location = new System.Drawing.Point(3, 3);
                DGV.Name = "dataGridView1";
                DGV.Size = new System.Drawing.Size(347, 233);
                DGV.TabIndex = 10;

                TB.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)
            | System.Windows.Forms.AnchorStyles.Right)));
                TB.Location = new System.Drawing.Point(0, 242);
                TB.Name = "textBox5";
                TB.Size = new System.Drawing.Size(350, 20);
                TB.TabIndex = 0;
                TB.KeyUp += new System.Windows.Forms.KeyEventHandler(this.textBox5_KeyUp);
            }
        }
        private EditMode formMode;
        public EditMode FormMode
        {
            get { return formMode; }
            set { formMode = value; }
        }
        private Int32 idUser;
        public Int32 IdUser
        {
            get { return idUser; }
            set { idUser = value; }
        }
        private BindingSource BS = new BindingSource();
        private BindingSource BS1 = new BindingSource();
        private void AddEditUser_Load(object sender, EventArgs e)
        {
            BS.DataSource = MRF.ReturnDataSetDB(ConnectionString, StoredProcedures[0] + " " + IdUser);
            if (FormMode == EditMode.AddIn)
            {
                BindingSource BS2 = new BindingSource();
                Text = "Добавление пользователя";
                DataSet DS = new DataSet();
                DataTable dt = MRF.ReturnDataSetDB(ConnectionString, StoredProcedures[2], "SUser").Tables[0].Copy();
                DS.Tables.Add(dt);
                DataTable dt1 = MRF.ReturnDataSetDB(ConnectionString, StoredProcedures[0], "DUser").Tables[0].Copy();
                DS.Tables.Add(dt1);
                DS.Relations.Add("SDUser", DS.Tables["SUser"].Columns["Логин"], DS.Tables["DUser"].Columns["Логин"]);

                BS1.DataSource = DS;
                BS1.DataMember = "SUser";

                BS2.DataSource = BS1;
                BS2.DataMember = "SDUser";

                DGV.DataSource = BS1;

                textBox1.DataBindings.Add("Text", BS2, "Имя");
                textBox2.DataBindings.Add("Text", BS2, "Фамилия");
                textBox3.DataBindings.Add("Text", BS2, "Отчество");
                textBox4.DataBindings.Add("Text", BS2, "Должность");                
            }
            if (FormMode == EditMode.Editing)
            {
                Text = "Редактирование данных пользователя";
                textBox1.DataBindings.Add("Text", BS, "table.Имя");
                textBox2.DataBindings.Add("Text", BS, "table.Фамилия");
                textBox3.DataBindings.Add("Text", BS, "table.Отчество");
                textBox4.DataBindings.Add("Text", BS, "table.Должность");

                acceptButton1.Enabled = false;
            }
            if (FormMode == EditMode.Deleting)
            {
                Text = "Удаление пользователя";
                textBox1.Enabled = false;
                textBox2.Enabled = false;
                textBox3.Enabled = false;
                textBox4.Enabled = false;
                textBox1.DataBindings.Add("Text", BS, "table.Имя");
                textBox2.DataBindings.Add("Text", BS, "table.Фамилия");
                textBox3.DataBindings.Add("Text", BS, "table.Отчество");
                textBox4.DataBindings.Add("Text", BS, "table.Должность");
            }
        }

        private void acceptButton1_Click(object sender, EventArgs e)
        {
            if (FormMode == EditMode.Editing)
            {
                MRF.ExecuteSqlCommand(StoredProcedures[1] + " " + IdUser + ",'" + textBox1.Text.Trim() + "','" + textBox2.Text.Trim() + "','" + textBox3.Text.Trim() + "','" + textBox4.Text.Trim() + "'", ConnectionString);
            }
            if (FormMode == EditMode.AddIn)
            {
                if (Convert.ToInt32(MRF.ReturnDataSetDB(ConnectionString, "select count(*) from Users where Login='" + DGV.CurrentRow.Cells["Логин"].Value.ToString()+"'").Tables[0].Rows[0][0]) != 0)
                    MessageBox.Show("Данный пользователь существует!!! Добавление невозможно!!!","Ошибка!!!",MessageBoxButtons.OK,MessageBoxIcon.Error);
                else
                    MRF.ExecuteSqlCommand(StoredProcedures[3] + " '" + DGV.CurrentRow.Cells["Логин"].Value.ToString() + "','" + textBox1.Text.Trim() + "','" + textBox2.Text.Trim() + "','" + textBox3.Text.Trim() + "','" + textBox4.Text.Trim() + "'", ConnectionString);
            }
            if (FormMode == EditMode.Deleting)
            {
                MRF.ExecuteSqlCommand(StoredProcedures[4]+" "+IdUser,ConnectionString);
            }
        }
        private void textBox5_KeyUp(object sender, KeyEventArgs e)
        {
            BS1.Filter = "[Логин] like '%" + TB.Text.Trim() + "%'";
        }
        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            if (FormMode == EditMode.AddIn)
            {
                if (textBox1.Text.Trim().Length == 0 && textBox2.Text.Trim().Length == 0 && textBox3.Text.Trim().Length == 0 && textBox4.Text.Trim().Length==0)
                    acceptButton1.Enabled = false;
                else
                    acceptButton1.Enabled = true;
            }
            if (FormMode == EditMode.Editing)
                acceptButton1.Enabled = true;
        }
        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            if (FormMode == EditMode.Editing)
                acceptButton1.Enabled = true;
        }
        private void textBox3_TextChanged(object sender, EventArgs e)
        {
            if (FormMode == EditMode.Editing)
                acceptButton1.Enabled = true;
        }
        private void textBox4_TextChanged(object sender, EventArgs e)
        {
            if (FormMode == EditMode.Editing)
                acceptButton1.Enabled = true;
        }
    }
}
